//
//  ReviewListViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/6.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ReviewListViewController.h"
#import <MJRefresh.h>
#import "ReviewListCollectionViewCell.h"
#import "ReviewDetailViewController.h"

@interface ReviewListViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection1;
@property (strong, nonatomic) UICollectionView *collection2;
@property (strong, nonatomic) NSMutableArray *array1;
@property (strong, nonatomic) NSMutableArray *array2;
@property (strong, nonatomic) MetaModel *meta1;
@property (strong, nonatomic) MetaModel *meta2;
@property (strong, nonatomic) UISegmentedControl *control;
@end

@implementation ReviewListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array1 = [NSMutableArray array];
    self.array2 = [NSMutableArray array];
    
    [self setupView];
    [self loadData];
    [self loadData2];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"审核任务";
    
    self.control = [[UISegmentedControl alloc] initWithItems:@[@"待审核", @"已审核"]];
    _control.backgroundColor = COLOR(204, 204, 204, 1);
    _control.tintColor = COLOR(153, 153, 153, 1);
    _control.center = CGPointMake(ScreenWidth*0.5, XNavigationHeight+25.0);
    _control.selectedSegmentIndex = 0;
    [self.view addSubview:_control];
    [_control addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc] init];
    self.collection1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight+50.0, ScreenWidth, ScreenHeight-XNavigationHeight-50.0-XTabBarHeight) collectionViewLayout:layout1];
    _collection1.delegate = self;
    _collection1.dataSource = self;
    _collection1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection1];
    [_collection1 registerNib:[UINib nibWithNibName:@"ReviewListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ReviewListCollectionViewCell"];
    _collection1.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    UICollectionViewFlowLayout *layout2 = [[UICollectionViewFlowLayout alloc] init];
    self.collection2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight+50.0, ScreenWidth, ScreenHeight-XNavigationHeight-50.0-XTabBarHeight) collectionViewLayout:layout2];
    _collection2.tag = 1;
    _collection2.delegate = self;
    _collection2.dataSource = self;
    _collection2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection2];
    _collection2.hidden = YES;
    [_collection2 registerNib:[UINib nibWithNibName:@"ReviewListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ReviewListCollectionViewCell"];
    _collection2.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData2)];
}

- (void)valueChanged:(UISegmentedControl *)control {
    
    if (control.selectedSegmentIndex == 0) {
        _collection1.hidden = NO;
        _collection2.hidden = YES;
    }else {
        _collection1.hidden = YES;
        _collection2.hidden = NO;
    }
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_verify", @"status": @"0"} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array1 addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.collection1.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            weakself.meta1 = meta;
        }
        [weakself.collection1 reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)loadMoreData {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_verify", @"status": @"0", @"page": [NSString stringWithFormat:@"%d", _meta1.current_page+1]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array1 addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        weakself.meta1 = meta;
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.collection1.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            [weakself.collection1.mj_footer endRefreshing];
        }
        [weakself.collection1 reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        [weakself.collection1.mj_footer endRefreshing];
    }];
}

- (void)loadData2 {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_verify"} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array2 addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.collection2.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            weakself.meta2 = meta;
        }
        [weakself.collection2 reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)loadMoreData2 {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_verify", @"page": [NSString stringWithFormat:@"%d", _meta2.current_page+1]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array2 addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        weakself.meta2 = meta;
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.collection2.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            [weakself.collection2.mj_footer endRefreshing];
        }
        [weakself.collection2 reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        [weakself.collection2.mj_footer endRefreshing];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (collectionView.tag == 0) {
        return _array1.count;
    }else {
        return _array2.count;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth, 115.0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ReviewListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReviewListCollectionViewCell" forIndexPath:indexPath];
    NSDictionary *dic = nil;
    if (collectionView.tag == 0) {
        
        dic = _array1[indexPath.item];
        cell.imageView.hidden = YES;
    }else {
        
        dic = _array2[indexPath.item];
        cell.imageView.hidden = NO;
        if ([dic[@"status"] intValue] == -1) {
            cell.imageView.image = [UIImage imageNamed:@"refuse"];
        }else if ([dic[@"status"] intValue] == 1) {
            cell.imageView.image = [UIImage imageNamed:@"allow"];
        }else {
            cell.imageView.image = nil;
        }
    }
    if ([dic[@"verifyable_type"] isEqualToString:@"move"]) {
        cell.label1.backgroundColor = COLOR(106, 202, 37, 1);
        cell.label4.text = [NSString stringWithFormat:@"%@辆车申请移库至%@", dic[@"autocar_nums"], dic[@"to_parking"][@"name"]];
        cell.label5.text = @"移库时间";
        cell.label6.text = [NSString stringWithFormat:@"%@至%@", dic[@"start_time"], dic[@"end_time"]];
    }else if ([dic[@"verifyable_type"] isEqualToString:@"sellout"]) {
        cell.label1.backgroundColor = COLOR(252, 103, 61, 1);
        cell.label4.text = [NSString stringWithFormat:@"%@%@辆车售出", dic[@"parking"][@"name"], dic[@"autocar_nums"]];
        cell.label5.text = @"销售时间";
        cell.label6.text = dic[@"created_at"];
    }
    
    cell.label1.text = [NSString stringWithFormat:@" %@ ", dic[@"verifyable_type_label"]];
    cell.label2.text = dic[@"dealer"][@"name"];
    cell.label3.text = dic[@"created_at"];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ReviewDetailViewController *con = [[ReviewDetailViewController alloc] init];
    if (collectionView.tag == 0) {
        con._id = [_array1[indexPath.item][@"id"] intValue];
    }else {
        con._id = [_array2[indexPath.item][@"id"] intValue];
    }
    [self.navigationController pushViewController:con animated:YES];
}

@end
